<?php
namespace Borrow\Controller;

use Wechat\Controller\WxCustomerBaseController;

class IndexController extends WxCustomerBaseController {

    protected $borrowModel;
	
	function _initialize() {
		parent::_initialize();
		
		$this->borrowModel = D("Borrow/Borrow");
	}

	/*************************************** WX **************************************************/
	/** save */
	public function save() {
		// check card
	    $card_id = I('post.card_id');
	    $card = M('Card')->where(array("id" => $card_id))->find();
	    if (!$card) {
	        $data['code'] = 499;
	        $data['msg'] = '借阅失败，会员卡不存在！';
	        $this->ajaxReturn($data);
	    }
		if ($card['cur_borrow_count'] >= $card['max_borrow_count']) {
	        $data['code'] = 499;
	        $data['msg'] = '借阅失败，当前会员卡借阅数量已满！';
	        $this->ajaxReturn($data);
		}
		
		// check bookCode
	    $code = I('post.code');
	    $bookCode = M('BookCode')->where(array("code" => $code))->find();
	    if (!$bookCode) {
	        $data['code'] = 499;
	        $data['msg'] = '借阅失败，图书CODE不存在！';
	        $this->ajaxReturn($data);
	    }
	    
	    // check lv
	    $bookId = $bookCode['bid'];
	    $book = M('Book')->where(array("id" => $bookId))->find();
	    if (!$book) {
	        $data['code'] = 499;
	        $data['msg'] = '借阅失败，图书不存在！';
	        $this->ajaxReturn($data);
	    }
	    if ($card['lv'] < $book['lv']) {
	        $data['code'] = 499;
	        $data['msg'] = '借阅失败，等级不足！';
	        $this->ajaxReturn($data);
	    }
		
		// check book
//		if ($book['remain_count'] == 0) {
// 	        $data['code'] = 499;
// 	        $data['msg'] = '借阅失败，图书已经借完！';
// 	        $this->ajaxReturn($data);
//		}
	    
		// 如果图书正在被借阅，判断是否已经借阅，否则先归还再借阅
		$book_code_id = $bookCode['id'];
	    $borrow = $this->borrowModel->where("book_code_id = $book_code_id and status < 2")->find();
		if ($borrow) {
			if ($borrow['status'] == 0) {
    	        $data['code'] = 499;
    	        $data['msg'] = '借阅失败，该书等待管理员确认！';
    	        $this->ajaxReturn($data);
			} else if ($borrow['status'] == 1) {
    	        $data['code'] = 499;
    	        $data['msg'] = '借阅失败，该书已被借阅！';
    	        $this->ajaxReturn($data);
			}
		}
		
		// 会员卡当前借阅 + 1
		M('Card')->where(array("id" => $card_id))->setInc('cur_borrow_count', 1);
		
		// 图书剩余 - 1
		M('Book')->where(array("id" => $card_id))->setDec('remain_count', 1);
		
		// add
	    $item = [
	        'card_id' => $card_id,
	        'card_no' => $card['no'],
	        'card_owner_name' => $card['owner_name'],
	        'book_code_id' => $bookCode['id'],
	        'book_code' => $bookCode['code'],
	        'book_id' => $book['id'],
	        'book_name' => $book['name'],
	        'book_thumb' => $book['thumb'],
	        'status' => 0,
	        'add_time' => date("Y-m-d H:i:s"),
	        'update_time' => date("Y-m-d H:i:s"),
	    ];
	     
	    $result = $this->borrowModel->add($item);
	    if (!$result) {
	        $data['code'] = 499;
	        $data['msg'] = '借阅出错，未知错误！';
	        $this->ajaxReturn($data);
        }

        $data['code'] = 0;
        $data['msg'] = '借阅成功!';
        $data['obj'] = $item;
        $this->ajaxReturn($data);
	}
	
    /** 查看某信息ByCode */
	public function findByCode() {
	    $code = I('code');
	    $borrow = $this->borrowModel->where(array("book_code" => $code))->find();
	    if (!$borrow) {
	        $data['code'] = 499;
	        $data['msg'] = '借阅信息不存在!';
	        $this->ajaxReturn($data);
	    }

	    $data['code'] = 0;
	    $data['msg'] = '找到目标!';
	    $data['obj'] = $borrow;
	    $this->ajaxReturn($data);
    }
	
	/** getCustBorrowList */
	public function getCustBorrowList() {
		$customer = $this->customer;
	    $this->assign('customer', $customer);
	    $this->display(':cust-borrow-list');
	    return;
	}
	
	/** 删除某信息 */
	public function deleteBorrow() {
		$id = $_GET['id'];
		
		$borrow = $this->borrowModel->where(array("id" => $id))->find();
		if (!$borrow) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
		// 更新status & update
		$borrow['status'] = -1;

		$result = $this->borrowModel->where(array("id" => $id))->save($borrow);
		if (false === $result) {
			$data['code'] = 499;
			$data['msg'] = '删除失败！';
			$this->ajaxReturn($data);
		}

		$updatedPost = $this->borrowModel->where(array("id" => $id))->find();
        
		$data['code'] = 0;
		$data['msg'] = '删除成功！';
		$this->ajaxReturn($data);
	}
	
	/** returnBook */
	public function returnBook() {
		$id = $_GET['id'];
        
		$borrow = $this->borrowModel->where(array("id" => $id))->find();
		if (!$borrow) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
		if ($borrow['status'] != 1) {
			$data['code'] = 499;
			$data['msg'] = '借阅失败，不是借阅状态！';
			$this->ajaxReturn($data);
		}
		
		// 图书剩余 + 1
		M('Book')->where(array("id" => $borrow['book_id']))->setInc('remain_count', 1);

		// 会员卡当前借阅 - 1
		M('Card')->where(array("id" => $borrow['card_id']))->setDec('cur_borrow_count', 1);

		// 更新$borrow
		$borrow['status'] = 2;
		$borrow['end_time'] = date("Y-m-d H:i:s");
		$result = $this->borrowModel->where(array("id" => $id))->save($borrow);
        
		$data['code'] = 0;
		$data['msg'] = '处理成功！';
		$data['obj'] = $borrow;
		$this->ajaxReturn($data);
	}
    
}
